package com.songlanyun.modules.platformgivepoint.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.enums.BillConstant;
import com.songlanyun.common.enums.IntegralConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.DateUtils;
import com.songlanyun.common.utils.custom.MathUtils;
import com.songlanyun.modules.account.entity.AccountEntity;
import com.songlanyun.modules.account.entity.UserIdentityAssetEntity;
import com.songlanyun.modules.account.entity.UserInfoEntity;
import com.songlanyun.modules.account.entity.UserVerifiedEntity;
import com.songlanyun.modules.account.service.AccountService;
import com.songlanyun.modules.account.service.UserInfoService;
import com.songlanyun.modules.account.service.UserVerifiedService;
import com.songlanyun.modules.account.service.impl.UserIdentityAssetServiceImpl;
import com.songlanyun.modules.integralrecord.entity.IntegralRecord;
import com.songlanyun.modules.integralrecord.service.IntegralRecordService;
import com.songlanyun.modules.platformgivepoint.entity.PlatformGivePointRecordDto;
import com.songlanyun.modules.pointconfig.entity.PointConfig;
import com.songlanyun.modules.pointtobalace.entity.PointToBalanceRecordEntity;
import com.songlanyun.modules.pointtobalace.entity.PointToBalanceStatistics;
import com.songlanyun.modules.pointtobalace.service.PointToBalanceRecordService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.platformgivepoint.dao.PlatformGivePointRecordDao;
import com.songlanyun.modules.platformgivepoint.entity.PlatformGivePointRecordEntity;
import com.songlanyun.modules.platformgivepoint.service.PlatformGivePointRecordService;


@Service("platformGivePointRecordService")
public class PlatformGivePointRecordServiceImpl extends ServiceImpl<PlatformGivePointRecordDao, PlatformGivePointRecordEntity> implements PlatformGivePointRecordService {


    @Autowired
    private  PointToBalanceRecordService pointToBalanceRecordService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private UserInfoService userInfoService;


    @Autowired
    private IntegralRecordService integralRecordService;

    @Autowired
    private UserVerifiedService userVerifiedService;
    @Override
    public PlatformGivePointRecordEntity create(PlatformGivePointRecordEntity entity) {
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public PlatformGivePointRecordEntity modify(PlatformGivePointRecordEntity entity) {
        return null;
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        LambdaQueryWrapper<PlatformGivePointRecordEntity> wrapper = new LambdaQueryWrapper<>();
        String realName = cn.hutool.core.map.MapUtil.getStr(params, "realName");
        String mobile = cn.hutool.core.map.MapUtil.getStr(params, "mobile");

        Date startTime = cn.hutool.core.map.MapUtil.getDate(params, "start_time");
        Date endTime = MapUtil.getDate(params, "end_time");

        wrapper.like(StringUtils.isNotBlank(realName),PlatformGivePointRecordEntity::getRealName,realName);
        wrapper.like(StringUtils.isNotBlank(mobile),PlatformGivePointRecordEntity::getMobile,mobile);

        wrapper.ge(ObjectUtil.isNotNull(startTime),PlatformGivePointRecordEntity::getCreateTime,startTime);
        if(ObjectUtil.isNotNull(endTime)) {
            endTime = DateUtils.addDateDays(endTime,1);
            wrapper.le(ObjectUtil.isNotNull(endTime), PlatformGivePointRecordEntity::getCreateTime, endTime);
        }


        wrapper.orderByDesc(PlatformGivePointRecordEntity::getCreateTime);
        IPage<PlatformGivePointRecordEntity> page = this.page(
                new Query<PlatformGivePointRecordEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }




    @Override
    public void give(PlatformGivePointRecordDto dto) {
        BigDecimal result = getCanGiveValue();
        if(dto.getGivePoint().compareTo(result)>0)
        {
            throw new RRException("最多可以赚与的积分为:"+result);
        }

        AccountEntity account = accountService.getByMobile(dto.getMobile());
        if(ObjectUtil.isNull(account))
        {
            throw new RRException("没有找到该手机号对应的用户");
        }

        String realName = "";
        UserVerifiedEntity optionUserVerifiedEntity = userVerifiedService.loadById(account.getId(), false);
        if(ObjectUtil.isNull(optionUserVerifiedEntity))
        {
            UserInfoEntity userInfoEntity = userInfoService.loadById(account.getId(), true);
            if(ObjectUtil.isNotNull(userInfoEntity))
            {
                realName = StringUtils.isNotEmpty(userInfoEntity.getRealName())?userInfoEntity.getNickname():userInfoEntity.getRealName();
            }
        }
        else
        {
            realName = optionUserVerifiedEntity.getRealName();
        }

        //平台赠送
        integralRecordService.inner(new IntegralRecord(account.getId(), dto.getGivePoint(), IntegralConstant.InnerMode.PLATFORM_GIVE_POINT,
                IntegralConstant.IntegralModelType.PLATFORM_GIVE_POINT, account.getId(), "平台赠送积分"));


        PlatformGivePointRecordEntity entity = new PlatformGivePointRecordEntity();
        entity.setUserId(account.getId());
        entity.setRealName(realName);
        entity.setMobile(account.getMobile());
        entity.setGivePoint(dto.getGivePoint());
        this.create(entity);

    }

    @Override
    public BigDecimal getCanGiveValue() {
        PointToBalanceStatistics pointToBalanceStatistics = pointToBalanceRecordService.getPointToBalanceStatistics(new HashMap<>());
        BigDecimal totalGivePoint = pointToBalanceStatistics.getTotalGivePoint();
        BigDecimal totalPlatformGivePoint = getTotalGivePoint();
        BigDecimal result = MathUtils.setScaleDown(totalGivePoint.doubleValue() - totalPlatformGivePoint.doubleValue(), 8);
        return result;
    }

    @Override
    public BigDecimal getTotalGivePoint() {
        return this.baseMapper.getPlatformGivePointCount();
    }

}
